    package com.english_test_sys.controller;

    import com.english_test_sys.pojo.Category;
    import com.english_test_sys.pojo.User;
    import com.english_test_sys.pojo.UserWordRecord;
    import com.english_test_sys.pojo.Word;
    import com.english_test_sys.service.WordService;
    import jakarta.servlet.http.HttpSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;

    import java.util.List;

    @Controller
    @RequestMapping("/api/words")
    public class WordController {

        @Autowired
        private WordService wordService;


        @GetMapping("/word_practice")
        public String wordPracticePage() {
            return "word_practice";
        }
        @ResponseBody
        @GetMapping("/categories")
        public List<Category> getAllCategories() {
            return wordService.getAllCategories();
        }

        @ResponseBody
        @GetMapping("/category/{Id}")
        public List<Word> getWordsByCategory(@PathVariable ("Id") Integer categoryId) {
            return wordService.getWordsByCategory(categoryId);
        }
        @ResponseBody
        @PostMapping("/record-mistake")
        public ResponseEntity<?> recordMistake(@RequestBody UserWordRecord record) {
            // 查找是否已有记录
            UserWordRecord existingRecord = wordService.findByUserIdAndWordId(record.getUserId(), record.getWordId());

            if (existingRecord != null) {
                // 更新复习次数
                existingRecord.setReviewCount(existingRecord.getReviewCount() + 1);
                wordService.update(existingRecord);
                return ResponseEntity.ok(existingRecord);
            } else {
                // 创建新记录，默认复习次数为1
                record.setReviewCount(1);
                wordService.save(record);
                return ResponseEntity.ok(record);
            }
        }
        @ResponseBody
        @GetMapping("/mistake-words")
        public List<Word> getMistakeWords(HttpSession session) {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                throw new RuntimeException("用户未登录");
            }
            return wordService.getMistakeWordsByUserId(user.getId());
        }
        @GetMapping("/English_test_home")
        public String wrong_questions() {
            return "English_test_home";
        }
    }